/* 全局拓展对象 */

/*
** 名单拓展，当前有 绕房名单/白名单/盟友名单
*/

export default {
    
    hail(content:string = '',toPublic: boolean = true):string{
        Object.values(Game.creeps).forEach(creep=>creep.say(`${content}`,toPublic))
        return content ?content:'yeah!'
    },

    /* 绕过房间api */
    bypass: {
    
    /* 添加要绕过的房间 */
    add(roomNames: string):string {
        if (!Memory.bypassRooms) Memory.bypassRooms = []

        // 确保新增的房间名不会重复
        Memory.bypassRooms = _.uniq([ ...Memory.bypassRooms, roomNames])
        return `[bypass]已添加绕过房间 \n ${this.show()}`
    },

    show():string{
        if (!Memory.bypassRooms || Memory.bypassRooms.length <= 0) return '[bypass]当前无绕过房间'
        return `[bypass]当前绕过房间列表：${Memory.bypassRooms.join(' ')}`
    },
    clean():string{
        Memory.bypassRooms = []
        return `[bypass]已清空绕过房间列表，当前列表：${Memory.bypassRooms.join(' ')}`
    },
    remove(roomNames: string):string {
        if (!Memory.bypassRooms) Memory.bypassRooms = []
        if (roomNames.length <= 0) delete Memory.bypassRooms
        else Memory.bypassRooms = _.difference(Memory.bypassRooms,[roomNames])
        return `[bypass]已移除绕过房间${roomNames}`
    }
    },

    /* 白名单api */
    whitesheet:{
        add(username:string):string{
            if (!Memory.whitesheets) Memory.whitesheets = []
            Memory.whitesheets = _.uniq([...Memory.whitesheets,username])
            return `[whitesheet]已添加用户${username}进白名单！\n${this.show()}`
        },
        show():string{
            if (!Memory.whitesheets || Memory.whitesheets.length <= 0) return "[whitesheet]当前白名单为空！"
            return `[whitesheet]白名单列表：${Memory.whitesheets.join(' ')}`
        },
        clean():string{
            Memory.whitesheets = []
            return '[whitesheet]当前白名单已清空'
        },
        remove(username:string):string{
            // if (! (username in Memory.whitesheets)) return `[whitesheet]白名单里没有玩家“${username}”`
            if (!Memory.whitesheets) Memory.whitesheets = []
            if (Memory.whitesheets.length <= 0) delete Memory.whitesheets
            else Memory.whitesheets = _.difference(Memory.whitesheets,[username])
            return `[whitesheet]已移除${username}出白名单`
        }
    },

    /* 盟友名单api ---加入盟友名单的将自动加入白名单,但是删除盟友名单将不会删除其在白名单的数据 */
    friendsheet:{
        // 添加盟友列表
        add(username:string):string{
            if (!Memory.friendsheets) Memory.friendsheets = []
            if (!Memory.whitesheets) Memory.whitesheets = []
            Memory.whitesheets = _.uniq([...Memory.whitesheets,username])
            Memory.friendsheets = _.uniq([...Memory.friendsheets,username])
            return `[friendsheet]已添加用户${username}进盟友名单！\n${this.show()}`
        },
        show():string{
            if (!Memory.friendsheets || Memory.friendsheets.length <= 0) return "[friendsheets]当前盟友名单为空！"
            return `[friendsheet]盟友名单列表：${Memory.whitesheets.join(' ')}`
        },
        clean():string{
            Memory.friendsheets = []
            return '[friendsheet]当前盟友名单已清空'
        },
        remove(username:string):string{
            // if (! (username in Memory.friendsheets)) return `[friendsheets]盟友名单里没有玩家“${username}”`
            if (!Memory.friendsheets) Memory.friendsheets = []
            if (Memory.friendsheets.length <= 0) delete Memory.friendsheets
            else Memory.friendsheets = _.difference(Memory.friendsheets,[username])
            return `[friendsheets]已移除${username}出盟友名单`
        }
    },

    /* 获取爬虫记忆 */
    CM:{
        get(Name:string):string{
            if (!Game.creeps[Name]) return `[CN]不存在该爬虫`
            var result = `爬虫${Name}的记忆:\n`
            for (var i in Game.creeps[Name].memory)
            {
                result += i + ' : ' + Game.creeps[Name].memory[i] + '\n'
            }
            return result
        }
    },
    /* 框架房间管理名单 为统一框架进行房间管理的名单 */
    FR:{
        add(Name:string,plan:number,spawn1:string,role?:string[]):string{
            if (!Memory.frameRoomData) Memory.frameRoomData = {}
            var rd:RoomData = {name:Name,plan:plan,spawn1:spawn1,roleData:role?role:[]}
            Memory.frameRoomData[Name] = rd
            return `[FrameRoom]添加房间${Name}成功`
        },
        remove(Name:string):string
        {
            if (!Memory.frameRoomData) Memory.frameRoomData = {}
            if (Memory.frameRoomData[Name])
                delete Memory.frameRoomData[Name]
            return `[FrameRoom]刪除房间${Name}成功`
        }

    },
}